﻿using System;
using System.Web.UI.WebControls;
using Core.Authenticated;
using Core.Library;
using DataObject;
using DataObject.Adv;

namespace CMS.Controls.Adv
{
    public partial class AdvManager : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                ListAdvByTypeId();
        }

        private void ListAdvByTypeId()
        {                        
            var o = AdvController.Adv_GetAdvSingle();
            if (o != null)
                ltAdvSingle.Text = string.Format("<center><a href=\"{0}\"><img src=\"/Thumb.ashx?Id={1}&tId={2}&Size=700\" class=\"pt10 pb10\" alt=\"\"/></a></center>", o.Url, o.AdvId, (int)CategoryType.Adv);

            lvList.DataSource = AdvController.Adv_GetAdvMultiple();
            lvList.DataBind();

            lvSlideShow.DataSource = AdvController.Adv_GetAdvSlideShow();
            lvSlideShow.DataBind();
        }              

        protected void BtnSaveSingle(object sender, EventArgs e)
        {
            AdvController.Adv_DeleteByType((int)AdvType.Single);

            var o = new AdvObject();
            o.TypeId    = (int)AdvType.Single;
            o.Priority  = 1;
            o.ImagePath = txtImage.Text;
            o.Url       = txtUrl.Text;
            AdvController.Adv_Create(o);

            ListAdvByTypeId();
            ltMessage1.Text = MessageUtility.ShowMessage(null, "Cập nhật Banner thành công.", MessageType.Information); 
        }

        protected void BtnSaveMultiple(object sender, EventArgs e)
        {
            var lstAdvImage = txtImageList.Text;

            if (!string.IsNullOrEmpty(lstAdvImage))
            {
                var splitAdv = lstAdvImage.Split('►');

                if (splitAdv.Length > 0)
                {
                    AdvController.Adv_DeleteByType((int)AdvType.Multiple);

                    for (int i = 0; i < splitAdv.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(splitAdv[i]))
                        {
                            var o = new AdvObject();
                            o.TypeId    = (int)AdvType.Multiple;
                            o.Priority  = i + 1;
                            o.ImagePath = splitAdv[i];
                            o.Url       = string.Empty;
                            AdvController.Adv_Create(o);    
                        }
                    }

                    ListAdvByTypeId();
                }
            }
        }

        protected void BtnSaveSlideShow(object sender, EventArgs e)
        {
            var lstAdvImage = txtSlideShowList.Text;

            if (!string.IsNullOrEmpty(lstAdvImage))
            {
                var splitAdv = lstAdvImage.Split('►');

                if (splitAdv.Length > 0)
                {
                    AdvController.Adv_DeleteByType((int)AdvType.SlideShow);

                    for (int i = 0; i < splitAdv.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(splitAdv[i]))
                        {
                            var o = new AdvObject();
                            o.TypeId = (int)AdvType.SlideShow;
                            o.Priority  = i + 1;
                            o.ImagePath = splitAdv[i];
                            o.Url       = string.Empty;
                            AdvController.Adv_Create(o);    
                        }
                    }

                    ListAdvByTypeId();

                    ltMessage3.Text = MessageUtility.ShowMessage(null, "Cập nhật slide show thành công.", MessageType.Information);
                }
            }
        }

        protected void BtnUpdate(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lvList.Items)
            {
                var advId   = ConvertUtility.ToInt32(((HiddenField) item.FindControl("hfAdvId")).Value);
                var url     = ((TextBox) item.FindControl("txtUrl")).Text;

                if (advId > 0 && !string.IsNullOrEmpty(url))
                {
                    AdvController.Adv_Update_Url(advId, url);
                }
            }

            ListAdvByTypeId();

            ltMessage2.Text = MessageUtility.ShowMessage(null, "Cập nhật đường dẫn thành công.", MessageType.Information);
        }

        protected void lvList_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (string.Equals(e.CommandName, "DeleteAdv"))
            {
                var dataItem = (ListViewDataItem)e.Item;
                var id = ConvertUtility.ToInt32(lvList.DataKeys[dataItem.DisplayIndex].Value);

                if (id > 0)
                {
                    var o = AdvController.Adv_GetById(id);
                    if (o != null)
                    {
                        AdvController.Adv_Delete(o.AdvId);
                        ltMessage2.Text = MessageUtility.ShowMessage(null, "Xóa quảng cáo thành công.", MessageType.Information);                        
                    }
                }

                ListAdvByTypeId();
            }
        }

        protected void lvSlideShow_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (string.Equals(e.CommandName, "DeleteAdv"))
            {
                var dataItem = (ListViewDataItem)e.Item;
                var id = ConvertUtility.ToInt32(lvList.DataKeys[dataItem.DisplayIndex].Value);

                if (id > 0)
                {
                    var o = AdvController.Adv_GetById(id);
                    if (o != null)
                    {
                        AdvController.Adv_Delete(o.AdvId);
                        ltMessage2.Text = MessageUtility.ShowMessage(null, "Xóa slide show thành công.", MessageType.Information);
                    }
                }

                ListAdvByTypeId();
            }
        }        
    }
}